package whu.cn.whushare.common;

import android.app.Notification;
import android.content.Context;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import whu.cn.whushare.R;

public class UmengHandler {
    private static UmengHandler INSTANCE = new UmengHandler();
    private static Context mContext;
    private static PushAgent mPushAgent;

    private UmengHandler() {
    }

    public static UmengHandler getInstance(Context context) {
        mContext = context.getApplicationContext();
        if (mPushAgent == null) {
            mPushAgent = PushAgent.getInstance(mContext);
            mPushAgent.setDebugMode(true);
            mPushAgent.setNoDisturbMode(22, 0, 7, 0);
        }
        return INSTANCE;
    }

    public void setDefaultHandler() {
        setMessageHandler();
        setNotificationHandler();
    }

    public void setMessageHandler() {
        setMessageHandler(mDefaultMessageHandler);
    }

    public void setMessageHandler(UmengMessageHandler handler) {
        mPushAgent.setMessageHandler(handler);
    }

    public void setNotificationHandler(UmengNotificationClickHandler handler) {
        mPushAgent.setNotificationClickHandler(handler);
    }

    public void setNotificationHandler() {
        setNotificationHandler(mDefaultNotificationClickHandler);
    }

    private UmengMessageHandler mDefaultMessageHandler = new UmengMessageHandler() {
        @Override
        public void dealWithCustomMessage(final Context context, final UMessage msg) {
            new Handler(mContext.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    UTrack.getInstance(mContext).trackMsgClick(msg);
                    Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public Notification getNotification(Context context,
                                            UMessage msg) {
            switch (msg.builder_id) {
                case 1:
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                    RemoteViews myNotificationView = new RemoteViews(context.getPackageName(), R.layout.notification_view);
                    myNotificationView.setTextViewText(R.id.notification_title, msg.title);
                    myNotificationView.setTextViewText(R.id.notification_text, msg.text);
                    myNotificationView.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg));
                    myNotificationView.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg));
                    builder.setContent(myNotificationView);
                    Notification mNotification = builder.build();
                    //由于Android v4包的bug，在2.3及以下系统，Builder创建出来的Notification，并没有设置RemoteView，故需要添加此代码
                    mNotification.contentView = myNotificationView;
                    return mNotification;
                default:
                    //默认为0，若填写的builder_id并不存在，也使用默认。
                    return super.getNotification(context, msg);
            }
        }
    };

    private UmengNotificationClickHandler mDefaultNotificationClickHandler = new UmengNotificationClickHandler() {
        @Override
        public void dealWithCustomAction(Context context, UMessage msg) {
            Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
        }
    };

    public void release() {
        mContext = null;
        mPushAgent = null;
    }
}
